
var global_areas = [
[ "1",  "海淀一区",  "俄彦平", "empty" ],
[ "2",  "海淀二区",  "张冰" , "empty" ],  　
[ "3",  "朝阳/顺义区",  "吉日格乐", "empty"],  　
[ "4",  "昌平一区",  "尹家春", "empty"],
[ "5",  "宣化区",  "刘畅", "empty"],
[ "6",  "密云区",  "刘畅", "empty"],
[ "7",  "昌平二区", "刘畅", "empty"],
[ "8",  "天津区", "张凯", "empty"],
[ "9",  "超百区", "曹广田", "empty"],
[ "10", "通州/燕郊区", "韩世杰", "empty"], 　
[ "11", "房山区", "李月辉", "empty"], 　
[ "12", "石景山/门头沟区", "李高峰", "empty"], 　
[ "13", "丰台区", "孟令威", "empty"], 　
[ "14", "东西城区", "于辉", "empty"], 　
[ "15", "奥士凯", "于辉", "empty"], 　
[ "16", "大兴一区", "乔博", "empty"], 　
[ "17", "大兴二区", "陆强", "empty"], 　
[ "18", "S一区", "李泉利", "empty"], 　
[ "19", "S二区", "苏嘉", "empty"]

];

var global_shops =[
["1164","志新店","1","C","刘冀生","13311298961","( 116.374677, 39.99933)",null,null,null,null,null] ,
["1261","清河店","1","C","张耀祖","15210565055","( 116.358009, 40.035417)",null,null,null,null,null] ,
["1336","百旺店","1","B","章亦戊","13522150413","( 116.282455, 40.035327)",null,null,null,null,null] ,
["1337","安宁庄店","1","E2"," 赵兵辉","18911561314","( 116.331361, 40.055549)",null,null,null,null,null] ,
["1344","新都店","1","E2","刘军","18301595902","( 116.372013, 40.065807)",null,null,null,null,null] ,
["1454","西三旗店","1","E1","王威威","13520827377","( 116.344492, 40.05263)",null,null,null,null,null] ,
["1530","安河桥北店","1","E2","陈方珠","18515897218","( 116.275998, 40.030352)",null,null,null,null,null] ,
["1658","温泉品超市","1","","刘红","13126882688","( 116.952156, 40.96882)",null,null,null,null,null] ,
["1166","华天店","2","C","张振民","13910701981","( 116.342664, 39.903535)",null,null,null,null,null] ,
["1171","中关村店","2","D","张勤庆","13911641669","( 116.478462, 39.994327)",null,null,null,null,null] ,
["1323","花园路店","2","C","王双喜","13681100175","( 116.370262, 39.981216)",null,null,null,null,null] ,
["1333","增光路店","2","D","李楠","13810820847","( 116.329068, 39.934119)",null,null,null,null,null] ,
["1433","明光桥西店","2","D","高现峰","15313952636","( 116.368602, 39.980746)",null,null,null,null,null] ,
["1456","田村店","2","B","杜永德","13720032389","( 116.252992, 39.935205)",null,null,null,null,null] ,
["1477","联想桥店","2","B","郑卫华","13681070725","( 116.341551, 39.975252)",null,null,null,null,null] ,
["1562","万寿路店","2","E2","尹志忠","13522869806","( 116.301176, 39.922967)",null,null,null,null,null] ,
["1038","北洼路店","2","E1","凌陆远","13321120567","( 116.402443, 39.937339)",null,null,null,null,null] ,
["1153","崇菜花园路店","2","E2","高现峰","15313952636","( 116.381403, 39.98749)",null,null,null,null,null] ,
["1370","北太平庄店","2","E2","罗凯","15010300483","( 116.377567, 39.974658)",null,null,null,null,null] ,
["1632","紫竹院店","2","F","廖先举 ","13717997672","( 116.323827, 39.939652)",null,null,null,null,null] ,
["1163","惠新店","3","A","李纪冲","13810650592","( 116.423225, 39.99258)",null,null,null,null,null] ,
["1169","北苑店","3","C","孙家飞","13691166277","( 116.428955, 40.048222)",null,null,null,null,null] ,
["1341","管庄店","3","E1","刘井彪","18810155120","( 116.599194, 39.917139)",null,null,null,null,null] ,
["1373","顺义仁和店","3","D","陈付喜","15711290966","( 116.67284, 40.113888)",null,null,null,null,null] ,
["1377","望京方恒店","3","E1","胡海建","13366362558","( 116.488156, 39.995026)",null,null,null,null,null] ,
["1408","甘露园店","3","C","董可佳","18911216353","( 116.518955, 39.922787)",null,null,null,null,null] ,
["1451","远洋一方店","3","E2","包松涛","13671085786","( 116.609606, 39.905005)",null,null,null,null,null] ,
["1452","望京麒麟社店","3","","王孝诚","13683245165","( 116.485761, 40.005268)",null,null,null,null,null] ,
["1482","北苑东店","3","D","费月宝","15011074866","( 116.443472, 40.035189)",null,null,null,null,null] ,
["1492","后沙峪店","3","C","杨松","13701303152","( 116.547427, 40.105342)",null,null,null,null,null] ,
["1617","大成东店","3","C","王国振","15801533881","( 116.49817, 39.905511)",null,null,null,null,null] ,
["1622","立水桥南店","3","F","王国新","13911286641","( 116.417618, 40.049853)",null,null,null,null,null] ,
["1039","顺义胜利店","3","F","檀志强","13641291092","( 116.653477, 40.139809)",null,null,null,null,null] ,
["1364","北沙滩店","3","E2","刘文强","13811426259","( 116.380761, 40.007393)",null,null,null,null,null] ,
["1350","南口店","4","E2","徐韦立","18811431062","( 116.136323, 40.244947)",null,null,null,null,null] ,
["1465","昌平东关店","4","D","汪涛","15201335086","( 116.26478, 40.227508)",null,null,null,null,null] ,
["1573","沙河店","4","C","韩世然","18911232152","( 116.294776, 40.156275)",null,null,null,null,null] ,
["1574","昌平西关店","4","C","翟海晓","13261363051","( 116.229849, 40.226147)",null,null,null,null,null] ,
["1605","昌平品超市","4","F","王祥伟","13681174919","( 116.240293, 40.22937)",null,null,null,null,null] ,
["1016","昌平店","4","D","王祥伟","13681174919","( 116.23968, 40.230228)",null,null,null,null,null] ,
["1348","昌平二店","4","E2","翟海晓","13261363051","( 116.249565, 40.22651)",null,null,null,null,null] ,
["1490","宣化店","5","D","杨仲军","13463467788","( 116.415184, 39.92185)",null,null,null,null,null] ,
["1539","沙城店","5","D","杨晓东","15133384308","( 116.415184, 39.92185)",null,null,null,null,null] ,
["1577","怀来金泉店","5","E2","杨晓东","15133384308","( 115.634061, 40.347984)",null,null,null,null,null] ,
["1129","鼓楼店","6","B","马小波","13701394330","( 116.849399, 40.397911)",null,null,null,null,null] ,
["1310","密云果园店","6","C","王春彪","13436953706","( 116.836974, 40.379956)",null,null,null,null,null] ,
["1624","密云新中街店","6","D","王旭东","13716783634","( 116.451472, 39.950785)",null,null,null,null,null] ,
["1679","密云万象汇店","6",""," 张荣庆","18811000690","( 116.836733, 40.380528)",null,null,null,null,null] ,
["1028","天通苑店","7","A","刘大伟","18800055722","( 116.443794, 40.080366)",null,null,null,null,null] ,
["1248","回龙观东店","7","B","曹广周","13552569751","( 116.342261, 40.085686)",null,null,null,null,null] ,
["1347","回龙观一店","7","D","甄慧生","15810682818","( 116.329409, 40.082543)",null,null,null,null,null] ,
["1349","回龙观二店","7","C","高存良","13522393973","( 116.359129, 40.086728)",null,null,null,null,null] ,
["1352","新龙店","7","E1","苏靳旭","15011295352","( 116.334929, 40.064454)",null,null,null,null,null] ,
["1389","北七家店","7","B","戈禄福","18610691099","( 116.419342, 40.123044)",null,null,null,null,null] ,
["1512","三旗百汇店","7","B","孙朋","15011189277","( 116.346913, 40.06706)",null,null,null,null,null] ,
["1516","小汤山店","7","D","麻建元","15810898015","( 116.394445, 40.170057)",null,null,null,null,null] ,
["1431","温都水城店","7","F","孟爱军","18813170354","( 116.402443, 39.937339)",null,null,null,null,null] ,
["1660","金色漫香苑店","7",""," 乔凤龙","13520530888","( 116.438362, 40.140009)",null,null,null,null,null] ,
["1229","黄河道店","8","E1","王增","13512093919","( 116.415184, 39.92185)",null,null,null,null,null] ,
["1289","水木天成店","8","D","盛宝泉","13920277334","( 116.358543, 40.033679)",null,null,null,null,null] ,
["1290","中北店","8","D","周健","13672152311","( 116.415184, 39.92185)",null,null,null,null,null] ,
["1291","大港店","8","E1","赵元鸿","18622247301","( 116.415184, 39.92185)",null,null,null,null,null] ,
["1306","万新店","8","E1","张津","13682139991","( 116.42875, 39.808546)",null,null,null,null,null] ,
["1361","华苑店","8","E2","张敏","18322361333","( 116.415184, 39.92185)",null,null,null,null,null] ,
["1397","张家窝店","8","E1","孙宝权","18920941555","( 116.415184, 39.92185)",null,null,null,null,null] ,
["1421","静海店","8","E1","张宇","13920571810","( 116.986825, 38.837511)",null,null,null,null,null] ,
["1429","葛沽店","8","E2","张长旺","18302271129","( 116.415184, 39.92185)",null,null,null,null,null] ,
["1468","丁字沽店","8","D","张杰","13512272413","( 116.415184, 39.92185)",null,null,null,null,null] ,
["1473","杨柳青二店","8","D","张俊立","18102175333","( 116.415184, 39.92185)",null,null,null,null,null] ,
["1518","西南角店","8","E1","于增翔","13662079503","( 116.415184, 39.92185)",null,null,null,null,null] ,
["1531","小站店","8","E2","刘炳军","15602090875","( 116.415184, 39.92185)",null,null,null,null,null] ,
["1546","梅江店","8","E2","陈静","15022501537","( 116.115952, 24.29075)",null,null,null,null,null] ,
["1629","港东新城店","8",""," 丛磊","13920790741","( 116.415184, 39.92185)",null,null,null,null,null] ,
["1383","圣熙八号购物中心","9",""," 岳书猛","13911963153","( 116.360936, 40.014204)",null,null,null,null,null] ,
["1010","新街口新华百货","9",""," 李志国","15010110762","( 116.377632, 39.953565)",null,null,null,null,null] ,
["1365","学清路店","9","B","岳书猛","13911963153","( 116.361102, 40.014211)",null,null,null,null,null] ,
["1170","新街口店","9","A","李志国","15010110762","( 116.378122, 39.953366)",null,null,null,null,null] ,
["1035","潞苑店","10","E1","马国兴","13718680672","( 116.685728, 39.936152)",null,null,null,null,null] ,
["1279","通州梨园店","10","C","蔡波","15011598291","( 116.67327, 39.890184)",null,null,null,null,null] ,
["1292","新华联家园店","10","B","高丛颖","13910506026","( 116.647263, 39.899529)",null,null,null,null,null] ,
["1315","燕郊店","10","C","陈开文","15110028162","( 116.395645, 39.929986)",null,null,null,null,null] ,
["1464","土桥店","10","D","王彬","13671225626","( 116.669744, 39.872636)",null,null,null,null,null] ,
["1517","天洋城店","10","B","刘成涛","13811210679","( 116.415184, 39.92185)",null,null,null,null,null] ,
["1520","香河店","10","D","孟宪征","15210187947","( 117.093257, 39.800306)",null,null,null,null,null] ,
["1575","通州次渠店","10","E2","马文涛","13717863298","( 116.570059, 39.791678)",null,null,null,null,null] ,
["1609","燕郊东贸店","10","D","马敏敏","15201130698","( 116.415184, 39.92185)",null,null,null,null,null] ,
["1391","通州三间房店","10","E2","林亮","18501199630","( 116.705671, 39.812954)",null,null,null,null,null] ,
["1654","燕郊方舟店","10",""," 王旭林","15383866033","( 116.415184, 39.92185)",null,null,null,null,null] ,
["1353","房山北关店","11","C","孙海涛","15810574701","( 115.990235, 39.708379)",null,null,null,null,null] ,
["1355","房山南关店","11","D","李洪毅","18500396625","( 115.991274, 39.700247)",null,null,null,null,null] ,
["1354","良乡店","11","D","何岩","13718405056","( 116.142354, 39.737851)",null,null,null,null,null] ,
["1356","燕山店","11","E1","刘玉龙","18811339783","( 115.970693, 39.72781)",null,null,null,null,null] ,
["1357","燕化星城店","11","E1","孙北北","13651130522","( 116.068606, 39.716676)",null,null,null,null,null] ,
["1011","古城店","12","D","李江波","15300080750","( 116.192903, 39.917351)",null,null,null,null,null] ,
["1023","新隆店","12","D","张敬宇","18600541590","( 116.113413, 39.937791)",null,null,null,null,null] ,
["1378","双峪环岛店","12","A","王有伟","13671317823","( 116.120349, 39.941492)",null,null,null,null,null] ,
["1420","西黄村二店","12","A","王彪","13911351688","( 116.20721, 39.937674)",null,null,null,null,null] ,
["1457","太阳岛店","12","C","李春","13901205238","( 116.207321, 39.914151)",null,null,null,null,null] ,
["1538","冯村店","12","D","马成相","13681394968","( 116.119444, 39.919236)",null,null,null,null,null] ,
["1579","沁山水店","12","C","范鹏飞","13301376228","( 116.415184, 39.92185)",null,null,null,null,null] ,
["1014","八角北里店","12","E1","李江波","15300080750","( 116.20543, 39.920999)",null,null,null,null,null] ,
["1021","云岗店","13","B","刘德文","13810859889","( 116.170712, 39.815862)",null,null,null,null,null] ,
["1024","北大地店","13","D","卞强","18911593280","( 116.28914, 39.860632)",null,null,null,null,null] ,
["1264","万源店","13","B","杨兴振","13718379323","( 116.430314, 39.809527)",null,null,null,null,null] ,
["1309","丰体时代店","13","C","范庆辉","18600139619","( 116.275433, 39.872196)",null,null,null,null,null] ,
["1313","玉蜓桥店","13","A","吕刚","13911163060","( 116.429171, 39.872006)",null,null,null,null,null] ,
["1325","马家堡店","13","E1","李伟","15110069962","( 116.384645, 39.849077)",null,null,null,null,null] ,
["1339","时代风帆店","13","C","赵廷儒","18310939376","( 116.377035, 39.852213)",null,null,null,null,null] ,
["1340","横一条店","13","E1","车承利","15210801685","( 116.440337, 39.855228)",null,null,null,null,null] ,
["1450","大成店","13","C","韩红军","13691524282","( 116.27869, 39.895374)",null,null,null,null,null] ,
["1505","草桥店","13","D","高国亮","13601187355","( 116.36563, 39.856994)",null,null,null,null,null] ,
["1552","杜家坎店","13","D","刘长宇","13701366522","( 116.216246, 39.851478)",null,null,null,null,null] ,
["1041","宋庄店","13","E2","李龙","13720085028","( 116.434624, 39.86003)",null,null,null,null,null] ,
["1312","开阳桥店","13","E2","李伟","15110069962","( 116.380217, 39.874325)",null,null,null,null,null] ,
["1628","丽泽桥店","13","D","路记尚","15001122816","( 116.31259, 39.872463)",null,null,null,null,null] ,
["1647","木樨园店","13","E1","陈雷","15010541581","( 116.405132, 39.869002)",null,null,null,null,null] ,
["1324","白纸坊店","14","D","贾沿海","18210051041","( 116.359273, 39.883187)",null,null,null,null,null] ,
["1328","德胜门店","14","D","祝卫涛","18801248587","( 116.391474, 39.957504)",null,null,null,null,null] ,
["1330","三里河店","14","B","郭建伟","15810580478","( 116.351332, 39.920992)",null,null,null,null,null] ,
["1332","和平新城店","14","C","付小军","18813057449","( 116.432158, 39.958264)",null,null,null,null,null] ,
["1334","安外店","14","E1","贾超","15801074635","( 116.412512, 39.967614)",null,null,null,null,null] ,
["1345","西安门店","14","D","吴敬伟","18401703738","( 116.380709, 39.928746)",null,null,null,null,null] ,
["1434","陶然亭店","14","B","高从扬","13693333320","( 116.382209, 39.884363)",null,null,null,null,null] ,
["1565","中信城店","14","F","隋彬","18310522235","( 116.379261, 39.881443)",null,null,null,null,null] ,
["1582","光明楼店","14","D","刘阿峰","13520048433","( 116.444936, 39.890451)",null,null,null,null,null] ,
["1015","新风店","14","E2","徐滨滨","13693667665","( 116.383468, 39.968695)",null,null,null,null,null] ,
["1635","金宝街店","14","F","钟兴宝","13810474317","( 116.443746, 39.925356)",null,null,null,null,null] ,
["1267","朝菜店","奥士凯","C","闫文权","15911021389","( 116.502838, 39.922027)",null,null,null,null,null] ,
["1251","交道口店","奥士凯","F","陈桂军","13581795802","( 116.42358, 39.935764)",null,null,null,null,null] ,
["1252","新兴里店","奥士凯","F","田进超","15901103002","( 116.392398, 39.885576)",null,null,null,null,null] ,
["1027","建兴店","16","C","薛强","13910927185","( 116.341896, 39.728483)",null,null,null,null,null] ,
["1167","大兴店","16","A","闫治江","18811183958","( 116.338844, 39.732836)",null,null,null,null,null] ,
["1305","北兴路店","16","D","无","无","( 116.342247, 39.782834)",null,null,null,null,null] ,
["1314","枣园店","16","C","郭佳刚","13520901373","( 116.342529, 39.754945)",null,null,null,null,null] ,
["1427","兴华大街店","16","D","李华","13581684044","( 116.344812, 39.728351)",null,null,null,null,null] ,
["1443","高米店北店","16","C","张伟","13810836856","( 116.342233, 39.782543)",null,null,null,null,null] ,
["1469","海子角店","16","E1","杨小英","17600570056","( 116.353243, 39.720617)",null,null,null,null,null] ,
["1593","大兴龙湖店","16","C","吴涛磊","13911802127","( 116.329198, 39.691154)",null,null,null,null,null] ,
["1025","旧宫店","17","C","田野","13910862029","( 116.450077, 39.81093)",null,null,null,null,null] ,
["1335","亦庄店","17","C"," 李帅","13718011856","( 116.504922, 39.797499)",null,null,null,null,null] ,
["1439","京良路店","17","C","徐书军","18701477207","( 116.197495, 39.813031)",null,null,null,null,null] ,
["1485","泰河园店","17","C","李丙太","13601266186","( 116.415184, 39.92185)",null,null,null,null,null] ,
["1571","固安店","17","E1","孔凡光","13601245261","( 116.324534, 39.516374)",null,null,null,null,null] ,
["1560","任丘源平店","17","E2","刘秋峰","15831895241","( 116.163214, 38.741105)",null,null,null,null,null] ,
["1595","富兴国际店","17","","吴海波","13911291887","( 116.5057, 39.807126)",null,null,null,null,null] ,
["1012","西门店","18","E2","史高","13439001605","( 116.650401, 39.912401)",null,null,null,null,null] ,
["1032","右安门店","18","F","党艳召","13520675944","( 116.370866, 39.865869)",null,null,null,null,null] ,
["1326","角门店","18","F","陈庆军","13366022524","( 116.402443, 39.937339)",null,null,null,null,null] ,
["1387","通州龙湖店","18","F","林亮","18501199630","( 116.402443, 39.937339)",null,null,null,null,null] ,
["1507","米拉小镇店","18","F","杨庆成","15910393306","( 116.402443, 39.937339)",null,null,null,null,null] ,
["1522","团河店","18","F","贺省卫","15911102246","( 116.402443, 39.937339)",null,null,null,null,null] ,
["1631","朝阳路店","18","F","陈筱一","13121339317","( 116.522631, 39.922383)",null,null,null,null,null] ,
["1633","潘家园店","18","F","杨立东","13701059045","( 116.472663, 39.881997)",null,null,null,null,null] ,
["1301","西山枫林店","19","F","李响","15901559228","( 116.207265, 39.948364)",null,null,null,null,null] ,
["1343","鲁谷店","19","F","杨东","15110213856","( 116.402443, 39.937339)",null,null,null,null,null] ,
["1588","万寿路南口店","19","F","李艳辉","18710119660","( 116.30147, 39.92414)",null,null,null,null,null] ,

[ "test1", "wesine测试 天安门店" , "1", 'A',  'tester',  '18009188888', '(116.403963,39.915119)',
     [ { layout_pic_url: 'http://imagestore-1255572104.cos.ap-beijing.myqcloud.com/layout/wm15_未命名.jpg',
         name: '未命名' } ],
     { '9' : { l: 0, x: 657, y: 843 },
       '10': { l: 0, x: 657, y: 709 },
       '11': { l: 0, x: 494, y: 863 },
       '12': { l: 0, x: 236, y: 822 },
       '13': { l: 0, x: 699, y: 163 },
       '15': { l: 0, x: 458, y: 197 },
       '16': { l: 0, x: 88,  y: 327  },
       '18': { l: 0, x: 125, y: 598 },
       '19': { l: 0, x: 89,  y: 737  },
       '20': { l: 0, x: 864, y: 770 },
       '22': { l: 0, x: 851, y: 356 },
       '23': { l: 0, x: 265, y: 198 }
     },
     //cameras
     {
       '192.168.1.150#4': 12,
       '192.168.1.150#5': 23
     },
     //nvrs
    {
      '192.168.1.150': { port: 80, username: "admin" , password: "admin1234" }
    },
    null
],

[ "test2",  "wesine测试　中南海店" , "3",  'A', 'tester', '1899198888', '(116.392953,39.921133)' ,
     [ { layout_pic_url: 'http://imagestore-1255572104.cos.ap-beijing.myqcloud.com/layout/wm15_未命名.jpg',
         name: '未命名' } ],
     { '9' : { l: 0, x: 657, y: 843 },
       '10': { l: 0, x: 657, y: 709 },
       '11': { l: 0, x: 494, y: 863 },
       '12': { l: 0, x: 236, y: 822 },
       '13': { l: 0, x: 699, y: 163 },
       '15': { l: 0, x: 458, y: 197 },
       '16': { l: 0, x: 88,  y: 327  },
       '18': { l: 0, x: 125, y: 598 },
       '19': { l: 0, x: 89,  y: 737  },
       '20': { l: 0, x: 864, y: 770 },
       '22': { l: 0, x: 851, y: 356 },
       '23': { l: 0, x: 265, y: 198 }
     },
     {
       '192.168.1.150#4': 12,
       '192.168.1.150#5': 23
     },
     {
         '192.168.1.150': { port: 80, username: "admin" , password: "admin1234" }
     },
     null
]

];




var global_protectors = [
    [ "test1", '112233445566', '10.10.0.11', { name: "测试假设备A", type: "fks"} ],
    [ "test2", '222233445566', '10.10.0.12', { name: "测试假设备B", type: 'fks'} ],
];

var global_users = [
    ['wm01测试用户', '12345678', false, 'admin', null],
    ['wm02测试用户', '22345678', false, 'admin', null]
    ['管理员用户',   '33345678', true, 'wm027', null]


];

async function createDatabase ( dbms ) {

    return await dbms.none (
'CREATE  TABLE public.areas( '+
'    id VARCHAR( 8 ),' +
'    name VARCHAR( 128 ),'+
'    charger VARCHAR( 128 ),'+
'    contact VARCHAR( 128 )' +
'); '+
'CREATE  TABLE public.protectors ( '+
'    shopid varchar( 128),'+
'    detail jsonb,'+
'    mac   varchar( 32 ) primary key,'+
'    ip    inet'+
');'+
'CREATE TABLE public.shops'+
'('+
'    id varchar(50) primary key,'+
'    name varchar(64),'+
'    areaId varchar(32),'+
'    category varchar(32),'+
'    charger  varchar(32),'+
'    contract  varchar(64),'+
'    location  point,'+
'    layers jsonb,'+
'    arm_areas jsonb,'+ // { idx: { a: , o: , t }  }
'    cameras jsonb, '+   //{ "ip#channel" : area_idx }
'    nvrs  jsonb,' + //{ ip: { port: , password: , username:  }  }
'    detail varchar(500)'+
');'+
'CREATE TABLE public.protector_op_log'+
'('+
'    stamp bigint primary key,'+
'    operation jsonb,'+
'    mac  varchar(20),'+
'    user_id varchar( 64)'+
');'+
'CREATE TABLE public.protector_alarm_log' +
'('+
'    stamp bigint primary key,'+
'    user_id varchar(64),' +
'    process_result varchar(64),' +
'    process_time bigint,' +
'    arm_area_idx varchar(64),'+
'    mac  varchar( 32),'+
'    shopid varchar(64)' +
');' +
'CREATE TABLE public.admin_user' +
'('+
'   name varchar(64),'+
'   mobile_phone varchar(64),'+
'   is_admin bool,' +
'   password varchar(64),' +
'   last_visited bigint'  +
')'
);
}

var pgpromise = require( 'pg-promise' )()

const pgConfig = {
    user: 'postgres',
    database: 'postgres',
    password: '123456',
    host: '127.0.0.1',
    port: '5432',
    poolSize: 5,
}


async function main () {

    var db = pgpromise( pgConfig )

    console.log( db );

    try {
        await db.none( 'drop table areas;' )
    } catch ( e ) {
        console.log( "drop error: " , e )
    }

    try {
        await db.none( 'drop table shops;' )
    } catch ( e ) {
        console.log( "drop error: " , e )
    }

    try{
        await db.none( 'drop table protectors;')
    } catch ( e ) {
        console.log( "drop error: " , e )
    }
    try{
        await db.none( 'drop table protector_op_log;' )
    } catch ( e ) {
        console.log( "drop error: " , e )
    }


    try{
        await db.none( 'drop table protector_alarm_log;' )
    } catch ( e ) {
        console.log( "drop error: " , e )
    }

    try{
        await db.none( 'drop table admin_user;' )
    } catch ( e ) {
        console.log( "drop error: " , e )
    }

    try{
        await createDatabase( db )
	    console.log( "create database ok" );
    } catch ( e ) {
        console.log( "create error: " , e )
    }
/*
'CREATE  TABLE public.areas( '+
'    id VARCHAR( 8 ),' +
'    name VARCHAR( 128 ),'+
'    charger VARCHAR( 128 )'+
'); '+
*/
    try {
        for( let i = 0; i < global_areas.length ; i++ ) {
        console.log( "insert ", i, " area " )
          var d = global_areas[i]
          console.log( d );
          await db.none( "INSERT INTO areas( id, name, charger, contact )  VALUES( $1, $2, $3, $4 )", d );
        }
    }
    catch( e ) {
        console.log( "init shops failed: ", e )
    }


/* shops table
 *
    id varchar(50) primary key,'+
    name varchar(64),'+
    areaId varchar(32),'+
    category varchar(32),'+
    charger  varchar(32),'+
    contract  varchar(64),'+
    location  point,'+
    layers jsonb,'+
    arm_areas jsonb,'+
    cameras jsonb, '+   //{ "ip#channel" : area_idx }
    nvrs  jsonb' + //{ ip: { port: , password: , username:  }  }
    mac varchar(20),'//mac address
    details varchar(500)'+
*/
    try {
        for( let i = 0; i < global_shops.length ; i++ ) {
        console.log( "insert ", i, " shop" )
          var d = global_shops[i]
          console.log( d );
          await db.none( "INSERT INTO shops( id, name, areaId, category, charger, contract,  location, layers, arm_areas, cameras, nvrs, detail ) VALUES( $1, $2, $3, $4, $5, $6, $7, $8:json, $9, $10, $11, $12 )", global_shops[i] )
        }
    }
    catch( e ) {
        console.log( "init shops failed: ", e )
    }

/* protectors table
 *
    shopid varchar( 128),'+
    detail text,'+
    mac   macaddr primary key,'+
    ip    inet,'+
    arms json' +
*/

    for( let idx = 0; idx < global_protectors.length; idx++ ) {
        console.log( "------- insert protector ", idx , "th"  );
        await db.none( 'INSERT INTO protectors( shopid, mac, ip, detail ) values( $1, $2, $3, $4)', global_protectors[idx ] )
    }
    console.log( "all protectors: ", await db.query( "SELECT * from protectors " ) );
/*
 *   users table
 *
     id varchar(50) primary key,'+
     username varchar(50),'+
     password varchar(50) not null,'+
     phone varchar(11),'+
     email varchar(50),'+
     authorityId varchar(50)'+
*/



    try{
        for( let idx = 0; idx < global_users.length; idx++ ) {
            await db.query( 'insert into admin_user( name, mobile_phone, is_admin, password, last_visited ) VALUES( $1, $2, $3, $4, $5 ) ', global_users[idx] );
            console.log( 'insert user: ', global_users[idx] );
        }

    }
    catch( e ) {
        console.log( "insert user table error: ", e )
    }
}

main().then(function(data ){
	console.log( "success select : ")
	console.log( data );
}).catch(function( data ){
    console.log( "error: ", data )
})


